<template>
    <div  v-loading="loading"  style="background:white;width: 100%;max-height:100%;overflow: auto;padding:30px;margin:10px;font-size:12px;margin-bottom:50px;" >
      <el-row>
            <el-col :span="24" style="border:0px;">
                <div class="grid-content"  >
            <div style="text-align:center;margin:auto;font-size:30px;margin-bottom:20px;">{{title}}</div>
          </div>
        </el-col></el-row>
        <el-row  style="margin-bottom:20px" :gutter="20">
          <el-col :span="6" style="height:300px;text-align:center;align-items:center;align-content:center;justify-content: center; ">
            <div style="width:100%;">
               <img :src="url" style="width:90%;"/>
            </div>
            
          </el-col>

          <el-col :span="9" class="chart-col">
            <temperature-chart :isRotate="false"  :isLabelFormat="true" :x-data="bar[0].xDatas" :y-data="bar[0].yDatas" :unit="bar[0].unit" :barTitle="barTitle[0]"/>
          </el-col>
          <el-col :span="9" class="chart-col">
            <temperature-chart :isRotate="false"  :isLabelFormat="true" :x-data="bar[1].xDatas" :y-data="bar[1].yDatas" :unit="bar[1].unit" :barTitle="barTitle[1]"/>
          </el-col>
        </el-row>
        <el-row style="margin-bottom:20px">
            <el-col :span="24"  style="display:grid; grid-template-columns: repeat(9, 1fr);gap: 20px 5px;" >
                <div  v-for="(item,index) in one" :key="index">
                  <div style="text-align:center;align-items:center;align-content:center;justify-content: center; margin-bottom:20px " >
                  <div style="margin-bottom:15px;display:flex;width:100%;text-align:center;align-items:center;align-content:center;justify-content: center; ">
                    <div :style="{'align-content':'center',width:'85px',height:'38px',border: '1px solid black',color:getColorName(item)}">{{item.deviceName}}</div>
                  </div>
                  <div style="width:85px;height:110px;text-align:center;align-items:center;border-style:double;border-width:5px;border-color:black;text-wrap:wrap;margin:auto;font-size:13px">
                        <div style="margin-top:20px;display:flex;width:100%;text-align:center;align-items:center;align-content:center;justify-content: center; ">
                          <div style="width:70px;height:20px;line-height:20px;margin-top:10px;border: 1px solid white;align-content:center;background-color: #0E3170;font-weight: bolder;color:white;">{{item.remark}}</div>
                        </div>
                        <div style="margin-bottom:10px;margin-top:10px;display:flex;width:100%;text-align:center;align-items:center;align-content:center;justify-content: center; ">
                          <div > 单位： {{item.unit}}</div>
                        </div>
                    </div>
                   </div>
                </div>
             
          </el-col>
        </el-row>

        <el-row style="margin-bottom:20px">
          <el-col :span="10"  style="display:grid; grid-template-columns: repeat(1, 1fr);gap: 10px 10px;" >
                <div  v-for="(item,index) in data.kaig" :key="index">
                  <div style="display:flex;margin-left:10px;margin-top:5px; ">
                      <div   :style="{width:'50%',color:getColorName(item)}" >{{item.deviceName}}</div>
                      <div style="width:60%;padding:1px;display:flex;text-align:center;align-items:center;">
                        <div style="width:50%;height:20px;line-height:20px;border: 1px solid white;align-content:center;background-color: #0E3170;font-weight: bolder;color:white;">{{item.remark}}</div>
                        <div style="margin-left:5px;"> {{item.unit}}</div>
                      </div>
                     
                  </div>
              </div>
          </el-col>


          <el-col :span="14" style="display:grid; grid-template-columns: repeat(3, 1fr);gap: 10px 10px;" >
            <div v-for="(item,index) in data.moni" :key="index" style="width:100%">
              <div style="display:flex;margin-top:2px;width:100%;text-align:center;justify-content: start;">
                  <div>
                    <div :class="{'alarmDiv':getFlash(item) }"  :style="{'width':'20px','height':'20px','border-radius':'50%','background':getColor(item)}"></div>
                  </div>
                  <div  :style="{'margin-left':'10px',color:getColorName(item)}" >{{item.deviceName}}</div>
              </div>
            </div>
          </el-col>
        </el-row>
    </div>

</template>
<script>
import deviceImg from '@/assets/images/device.png'
import { mapState } from "vuex";
// import datas from '@/utils/test.json';
import TemperatureChart from './components/TemperatureChart.vue';

import {listDevs} from  "@/api/system/device";
import { histogramData } from "@/api/system/chartConfig";

// import elementResizeDetectorMaker from 'element-resize-detector'
// const erd = elementResizeDetectorMaker()
 

// import echarts from "echarts"
export default {
  components:{
  TemperatureChart
 },
name:"test",
mounted() {
  

      // this.$nextTick(()=>{
      //   console.log("11")
      //   if(this.chart==null){
      //     this.chart = this.$refs.bar
      //   }
      //   this.barChar = this.$echarts.init(this.chart)
       
      // }) 
  },
async created(){
      
  let res= await this.getConfigKey("sys.server.version")
   this.version =res.msg||this.version;
   try{
    this.url=require('@/assets/images/'+this.version+'_mainLeft.jpg')
   }catch(e){
   }

    this.title=("01"==this.type)?'左主机推进系统':("02==this.type")?'右主机推进系统':"主机推进系统"
    this.barTitle=("01"==this.type)?['左主机推进系统排气温度柱状图','左主机推进系统主轴承温度柱状图']:('02'==this.type)?['右主机推进系统排气温度柱状图','右主机推进系统主轴承温度柱状图']:[]
    this.devs=("01"==this.type)?this.devs_01:(("02"==this.type)?this.devs_02:this.dev)
    this.init();
      this.getData();
    if(null==this.timer){
      this.timer=  setInterval(() =>{
      this.init();
      this.getData();
     
    },2000);
    }
 
},
beforeDestroy() {
  if(this.timer){
      clearInterval(this.timer);
      this.timer=null;
    }
},

methods:{
//   getResize(){
//    this.myChart1.resize();
//  },
//  statusCharts(){
//   console.log(this.option)
//   this.myChart1.setOption(this.option)//你Echarts配置项
//  },
  getMark(item){
    return null ==item.remark?"":item.remark
  },
  getUnit(item){
    return item.unit
  },
  getName(item){
    // return"0"
    return item.deviceName
  },

  init(){
    listDevs({"deptId":this.deptId}).then(response=>{
      // console.log(this.datas)
      this.loading=false
      let dataArr =response.data;
      if(dataArr){
  
        if(this.type=='01'){//左
                this.getList(dataArr,"01K","01M")

        }else if(this.type=='02'){//右
                this.getList(dataArr,"02K","02M")
        }else{

        }
        this.one=dataArr.filter(item=>{
           return  this.devs.includes(item.systemId)
        }).sort((a,b)=> {
          return a.systemId.split("-")[1]-b.systemId.split("-")[1]}) 
      }
    })
  },
  getList(dataArr,f,v){
    this.data.moni= dataArr.filter(item=>{

          return   "MR" == item.modality  
          && item.systemId.startsWith(f)
        }).sort((a,b)=> {
          return a.systemId.split("-")[1]-b.systemId.split("-")[1]})

      this.data.kaig= dataArr.filter(item=>{
        return   "BA" == item.modality 
        && item.systemId.startsWith(v)
        //  && ( item.deviceName.startsWith(f) ||(v && !item.deviceName.startsWith(f) && !item.deviceName.startsWith("右")))
      }).sort((a,b)=> {
        return a.systemId.split("-")[1]-b.systemId.split("-")[1]})
  },
     getData(){
     let currentIds = this.currentId.split("/")

     currentIds.forEach(async (item,index)=>{
      
       await  histogramData({deviceGraphicalId:item}).then( async (response) =>
       {
         console.log("histogramData....")
         let datas=  await response.data;
         let dataList  = datas.data
         console.log(dataList)
         this.bar[index].xDatas=   dataList.map(item=> {return item.deviceName.substring(3)});
         this.bar[index].yDatas=   [{
                 type:'bar',
                 barWidth:'20',
                 data: dataList.map(item=> {return item.value})
             }]
  
      })
      
     })
     
    // console.log(111)
//        let xLabelWidth = ((this.barChar.getWidth()*1) -50-40)/ 10
// this.option.xAxis[0].axisLabel.width = xLabelWidth
      //  this.barChar.setOption(this.option,true);
    },
    getFlash(row){
        //2、报警消息时，闪烁
        let f = this.alarmList.some(alarm=>
            alarm ==row.deviceId
        );
        if(f){
          if(row.displayAlarm=="2"){//告警
            return true;
          }
        }
        if(this.lightSwitch=='1'  ||this.extExpSwitch=="1")
        {//1、试灯打开
          // console.log("extExpSwitch=="+this.extExpSwitch)
          if(row.displayAlarm=="2"){//告警
            return true;
          }
        }
          return false;
      },
      getColorName(row){
       //告警消息
     if("2"== row.displayAlarm){
      let f = this.alarmList.some(alarm=>
          alarm ==row.deviceId
        );
        if(f){
          return 'red';
        }

         //3、显示设备状态颜色
         if(row.warnStatus=='1'||row.warnStatus=='0'){
                        return 'black';
                      }
                      return 'red'


     }
     return "black";

      },
      getColor(row){
        // console.log(row)

             if("2"== row.displayAlarm){  //告警类型
                //告警消息
                let f = this.alarmList.some(alarm=>
                      alarm ==row.deviceId
                    );
                    if(f){//2、有报警通知，则显示灯的颜色
                      return row.lightColor;
                    }
                }
                if(this.lightSwitch=='1' ||this.extExpSwitch=="1"){//1、试灯打开，显示灯的颜色
                  // console.log("extExpSwitch=="+this.extExpSwitch)
                    return row.lightColor;
                }
                //3、显示设备状态颜色
                if(row.warnStatus=='1'||row.warnStatus=='0'){
                  return 'grey';
                }
                return row.lightColor;


      }
},
computed:{
    ...mapState("app", {   // 对象形式
        alarmList: "alarmList",
        lightSwitch:"lightSwitch",
        extExpSwitch:"extExpSwitch"
    }),
},
data() {
    return{
      url:"",
      chart:null,
      barChar:null,
      devs:[],
      devs_01:["01M-001","01M-002","01M-003","01M-005","01M-008","01M-009","01M-010","01M-011","01M-012"],
      devs_02:["02M-001","02M-002","02M-003","02M-005","02M-008","02M-009","02M-010","02M-011","02M-012"],
      myChart1:'',
      loading:true,
      barTitle:[],
      key1:"",
      key2:"",
      title:"",
      one:{deviceName:"",remark:"",unit:""},
      two:{deviceName:"",remark:"",unit:""},
      timer:null,
      data:{kaig:[],moni:[]},
      type:this.$route.query.type,
      deptId:this.$route.query.depCode,
      currentId:this.$route.query.graphId,//待定义displayAlarm
      img:deviceImg,
      animalv:"diagonal-line",
      animalv1:"diagonal-line1",
  
      name:this.$route.query.name,
        bar:[{
          timer:null,
          time:this.$route.query.time,
          // data: datas,
          yDatas:[],
          xDatas:[],
          unit:'°C'

        },{
          timer:null,
          time:this.$route.query.time,
          // data: datas,
          yDatas:[],
          xDatas:[],
          unit:'°C'
        }]
      }}

}
</script>
<style scoped >
.el-row {

&:last-child {
  margin-bottom: 0;
}
}

.el-col {
height: 100%;
/* border:1px solid; */
border-radius: 0px;
}
.circle{
border: 2px solid black;
width: 20px;
height: 20px;
display: inline-block;
border-radius: 50%;
/* background-color: white; */

font-size: 13px;
}
.bg-purple-dark {
/* background: #99a9bf; */
}
.bg-purple {
/* background: #d3dce6; */
}
.line {
  border-right: 2px solid #010101;
  display: inline-block;
  /* margin-bottom: -12px; */
}

.bg-purple-light {
/* background: #e5e9f2; */
}
.grid-content {
text-wrap:wrap;
font-size:clamp(0.7rem, 0.489rem + 1.05vw, 1.2rem);
}
.diagonal-line {
animation: rotate 1s linear infinite ;
animation-fill-mode:forwards;
animation-iteration-count: 1;
transform-origin: 0 0;
}

@keyframes rotate{
from{
transform: rotate(-40deg);
}
to{
transform: rotate(0deg);
}
}
.diagonal-line1 {
animation: rotate1 0.1s linear infinite ;
animation-fill-mode:forwards;
animation-iteration-count: 1;
transform-origin: 0 0;
}

@keyframes rotate1{
from{
transform: rotate(-0deg);
}
to{
transform: rotate(-40deg);
}
}
.item {
flex: 0 0 auto;
margin: 10px;
}
.row-bg {
padding: 10px 0;
/* background-color: #f9fafc; */
}

.alarmDiv{
      display:block;
      -webkit-animation-name: imageAnim;
      -webkit-animation-duration: 0.5s;
      -webkit-animation-iteration-count: infinite;
      -webkit-animation-direction: alternate;
      -webkit-animation-timing-function: ease;
      -webkit-animation-play-state: running;
  }
  @-webkit-keyframes imageAnim{
      0% {opacity: 1;}
      50% {opacity: 0.5;}
      100% {opacity: 0;}
  }
.echart-box {
  width: 100%;
  height: 100%;
  /* border: 3px solid pink; */
  /* margin: 20px auto; */
}  

</style>
